using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class manager_site_view : System.Web.UI.Page
{
    private string id = "";
    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["Admin"];
        if (user_present == null)
        {
            Response.Redirect("../login/login.aspx");
        }

        id = Request["id"];
        if (id == "new")
        {
            //button_save.ImageUrl = "../../images/create.gif";
            //button_save.Visible = true;
        }
        else
        {
           // button_save.ImageUrl = "../../images/update.gif";
            if (Page.IsPostBack == false)
            {
                BindData();
            }
            else return;
        }

    }

    protected void BindData()
    {
        SiteDomain site = SiteDomain.get_site(Convert.ToInt32(id));
        txt_baseDir.Text = site.BaseImageDir;
        txt_css.Text = site.CssFilename;
        txt_domain.Text = site.Domain;
        txt_siteCode.Text = site.SiteCode;
        txt_SiteDir.Text = site.SiteImageDir;
        if (site.IsDefault == true) RadioButtonList_default.Items[0].Selected = true ;
    }

    protected void execute(object sender, EventArgs e)
    {
        SiteDomain site;
        int adminID = Admin.get_admin(user_present).ID ;
        if (id == "new")
        {
            try
            {
                site = new SiteDomain();
                site.SiteCode = txt_siteCode.Text;
                site.SiteImageDir = txt_SiteDir.Text;
                site.Domain = txt_domain.Text;
                site.CssFilename = txt_css.Text;
                site.BaseImageDir = txt_baseDir.Text;
                if (RadioButtonList_default.Items[0].Selected == true)
                {
                    site.IsDefault = true;
                }
                else site.IsDefault = false;
                site.CreatedUserID = adminID;
                site.UpdatedUserID = adminID;
                site.Create();
                //ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('Create new site successful.');</script>");
                Functions.message_box(this,"Create successful.");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
        else
        {
            try
            {
                site = SiteDomain.get_site(Convert.ToInt32(id));
                site.SiteCode = txt_siteCode.Text;
                site.SiteImageDir = txt_SiteDir.Text;
                site.Domain = txt_domain.Text;
                site.CssFilename = txt_css.Text;
                site.BaseImageDir = txt_baseDir.Text;
                if (RadioButtonList_default.Items[0].Selected == true)
                {
                    site.IsDefault = true;
                }
                else site.IsDefault = false;
                site.UpdatedUserID = adminID;
                site.UpdatedDate = DateTime.Now;
                site.Update();
                Functions.message_box(this,"Update successful.");
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + ex.Message.ToString().Replace("'", "") + "');</script>");
            }
        }
    }
}
